package com.gemptc.daoimp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gemptc.bean.ReplyInfoBean;
import com.gemptc.bean.ReplyTravelsBean;
import com.gemptc.dao.IReplyTravels;
import com.gemptc.utils.DBUtil;

public class ReplyTravelsImp implements IReplyTravels {

	@Override
	public boolean insertTravels(int user_id, int reply_userid, int t_id,
			String r_content, String r_time, int r_rank) {
		Connection connection = DBUtil.getConnection();
		boolean flag = false;
		PreparedStatement preparedStatement = null;
		String sql = "insert into reply_travels (user_id,reply_userid,t_id,r_time,r_content,r_rank)"
				+ " values (?,?,?,?,?,?)";
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, user_id);
			preparedStatement.setInt(2, reply_userid);
			preparedStatement.setInt(3, t_id);
			preparedStatement.setString(4, r_time);
			preparedStatement.setString(5, r_content);
			preparedStatement.setInt(6, r_rank);
			preparedStatement.execute();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.release(null, preparedStatement, connection);
		}
		return flag;
	}

	@Override
	public boolean TwoinsertTravels(int rt_id, int user_id, int reply_userid,
			int t_id, String r_content, String r_time, int r_rank) {
		Connection connection = DBUtil.getConnection();
		boolean flag = false;
		PreparedStatement preparedStatement = null;
		String sql = "insert into reply_travels (rt_id,user_id,reply_userid,t_id,r_time,r_content,r_rank)"
				+ " values (?,?,?,?,?,?,?)";
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, rt_id);
			preparedStatement.setInt(2, user_id);
			preparedStatement.setInt(3, reply_userid);
			preparedStatement.setInt(4, t_id);
			preparedStatement.setString(5, r_time);
			preparedStatement.setString(6, r_content);
			preparedStatement.setInt(7, r_rank);
			preparedStatement.execute();
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DBUtil.release(null, preparedStatement, connection);
		}
		return flag;
	}

	@Override
	public List<ReplyTravelsBean> allReplyTravelsBeans(int t_id) {
		List<ReplyTravelsBean> list = new ArrayList<ReplyTravelsBean>();
		Connection connection = DBUtil.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		SelectImp selectImp = new SelectImp();
		String sql = "SELECT * FROM reply_travels WHERE reply_travels.`t_id`= ?;";
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, t_id);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int rt_id = resultSet.getInt(1);
				int user_id = resultSet.getInt(2);
				int reply_userid = resultSet.getInt(3);
				int t_id1 = resultSet.getInt(4);
				String r_time = resultSet.getString(5);
				String r_content = resultSet.getString(6);
				int r_rank = resultSet.getInt(7);
				String user_nickname = selectImp.select(user_id);
				String reply_usernickname = selectImp.select(reply_userid);
				ReplyTravelsBean replyTravelsBean = new ReplyTravelsBean(rt_id,
						user_id, reply_userid, t_id1, r_time, r_content,
						r_rank, user_nickname, reply_usernickname);
				list.add(replyTravelsBean);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.release(resultSet, preparedStatement, connection);
		}

		return list;
	}

}
